package com.fourhats.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fourhats.dao.PlaceDAO;
import com.fourhats.model.Place;

@Repository
public class PlaceDAOImpl extends GenericDAOImpl<Place, Long> implements PlaceDAO {

	protected PlaceDAOImpl() {
		super(Place.class.getName());
	}

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession(){
		return sessionFactory.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	public List<Place> getNearbyPlaces(double lat, double lng, float distance, double min_lat, double max_lat, double min_lng, double max_lng) {
		/*Query query = getCurrentSession().getNamedQuery("getNearbyPlaces")
				.setParameter("lat", "4.66655780")
				.setParameter("lng", "-74.05245210")
				.setParameter("dist", "1")
				.setParameter("min_lat", "4.66655780")
				.setParameter("max_lat", "4.66655780")
				.setParameter("min_lng", "-74.05245210")
				.setParameter("max_lng", "-74.05245210");*/
		
		Query query = getCurrentSession().getNamedQuery("getNearbyPlaces")
				.setParameter("lat", String.valueOf(lat))
				.setParameter("lng", String.valueOf(lng))
				.setParameter("dist", String.valueOf(distance))
				.setParameter("min_lat", String.valueOf(min_lat))
				.setParameter("max_lat", String.valueOf(max_lat))
				.setParameter("min_lng", String.valueOf(min_lng))
				.setParameter("max_lng", String.valueOf(max_lng));
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Place> getAllEnabledPlaces() {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
        criteria.add(Restrictions.eq("enable", true));
        return getHibernateTemplate().findByCriteria(criteria);
	}

}
